package com.makerworks.medu;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Interpolator;
import android.widget.FrameLayout;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * Created by liaoyuhao on 2016/7/26.
 */
public class WaveView extends FrameLayout {

    private static final String TAG = WaveView.class.getSimpleName();
    private static final float total_height = 962;
    private static final float center_radius_percent = 69 / total_height;
    private static final float end_radius_percent = 962 / total_height;
    private static final float duration = 6000;        //整个周期
    private static final int mSpeed = 1200;     //创建一个circle的间隔
    private final Paint mPaint = new Paint();
    private ArrayList<Circle> mCircles = new ArrayList<Circle>();
    private Interpolator mWaveInterpolator = new AccelerateInterpolator(0.65f);
    private boolean mIsRunning = false;

    private float mHeight, mWidth, mRadius;
    private int retryTime = 0;


    public WaveView(Context context) {
        super(context);
        init(context, null, 0);
    }

    public WaveView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs, 0);
    }

    public WaveView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }


    private void init(Context context, AttributeSet attrs, int defStyleAttr) {
        setWillNotDraw(false);
        mCircles.clear();
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.parseColor("#299dff"));
        mPaint.setStyle(Paint.Style.STROKE);
        start();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mHeight = h;
        mWidth = w;
        mRadius = h / 3 * 2;
        mPaint.setStrokeWidth(mHeight / 300);
    }

    private RectF tempRectF = new RectF();

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Iterator<Circle> iterator = mCircles.iterator();
        while (iterator.hasNext()) {
            Circle circle = iterator.next();
            long currentTime = System.currentTimeMillis();
            if (currentTime - circle.mCreateTime < duration) {
                mPaint.setAlpha(circle.getAlpha(currentTime));
                float height = getHeight();
                float width = getWidth();
                float radius = circle.getRadius(currentTime);
                tempRectF.left = width / 2 - radius;
                tempRectF.right = width / 2 + radius;
                tempRectF.top = height / 2 - radius;
                tempRectF.bottom = height / 2 + radius;
                canvas.drawArc(tempRectF, 0, 360, true, mPaint);
            } else {
                iterator.remove();
            }
        }
        if (mCircles.size() > 0) {
            postInvalidateDelayed(10);
        }
    }


    public void start() {
        if (!mIsRunning) {
            mIsRunning = true;
            mCreateCircleRunnable.run();
        }

    }

    public void stop() {
        mIsRunning = false;
    }

    private Runnable mCreateCircleRunnable = new Runnable() {
        @Override
        public void run() {
            if (mIsRunning) {
                newCircle();
                postDelayed(mCreateCircleRunnable, mSpeed);
            }
        }
    };

    private void newCircle() {
        long currentTime = System.currentTimeMillis();
        Circle circle = new Circle(currentTime);
        mCircles.add(circle);
        invalidate();
    }

    private class Circle {
        private long mCreateTime;

        public Circle(long createTime) {
            mCreateTime = createTime;
        }

        public int getAlpha(long currentTime) {
            float percent = (currentTime - mCreateTime) / duration;
            int alpha = (int) ((1 - mWaveInterpolator.getInterpolation(percent)) * 255 * 0.6);
            return alpha;
        }

        public float getRadius(long currentTime) {
            float percent = (currentTime - mCreateTime) / duration;
            float radius = (center_radius_percent + mWaveInterpolator.getInterpolation(percent) * (end_radius_percent - center_radius_percent)) * mRadius;
            return radius;
        }
    }

    //lyh 修复一个实际上没有泄露的内存泄露
    @Override
    protected void onWindowVisibilityChanged(int visibility) {
        if (visibility != VISIBLE) {
            removeCallbacks(mCreateCircleRunnable);
            mIsRunning = false;
        } else {
            start();
        }
        super.onWindowVisibilityChanged(visibility);
    }

}
